package com.voicechat.firstmet.adapter;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.voicechat.firstmet.R;
import com.voicechat.firstmet.bean.EmojiEntity;
import com.voicechat.firstmet.fragment.EmojiShowFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName: FirstMet
 * @Package: com.voicechat.firstmet.adapter
 * @ClassName: EmojiGvAdapter
 * @Description: 表情gridView  Adapter
 * @Author: ljp
 * @CreateDate: 2018/10/19 9:55
 * @Version: 1.0
 */
public class EmojiGvAdapter extends BaseAdapter{
    private Context mContext;
    private List<EmojiEntity> mEmojis;
    private int itemWidth;
    private String TAG = "EmojiGvAdapter";
    private OnEmojiClickListener onEmojiClickListener;

    public EmojiGvAdapter(Context context, List<EmojiEntity> datas, int page, int itemWidth) {
        Log.i(TAG, "EmojiGvAdapter: ----"+datas.size());
        mEmojis = new ArrayList<>();
        mContext = context;
        this.itemWidth = itemWidth;
        //start end分别代表要显示的数组在总数据List中的开始和结束位置
        int start = page * EmojiShowFragment.gridNum;
        int end = start + EmojiShowFragment.gridNum;
        while ((start < datas.size()) && (start < end)) {
            mEmojis.add(datas.get(start));
            start++;
        }
    }


    @Override
    public int getCount() {
        return mEmojis.size();
    }


    @Override
    public String getItem(int i) {
        return mEmojis.get(i).getUnicode();
    }
    public String getItemName(int i) {
        return mEmojis.get(i).getName();
    }
    @Override
    public long getItemId(int i) {
        return i;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (null == convertView) {
            holder = new ViewHolder();
            convertView = View.inflate(mContext, R.layout.item_emoji, null);
            holder.emojiTv = (TextView) convertView.findViewById(R.id.tv_emoji);
            holder.emojiImage = (ImageView) convertView.findViewById(R.id.image_emoji);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (mEmojis.get(position).getName().equals("del")) {
            //第21个显示删除按钮
            holder.emojiImage.setVisibility(View.VISIBLE);
            holder.emojiImage.setImageResource(R.mipmap.del);
        } else {
            holder.emojiTv.setText(getItem(position));
        }
        return convertView;
    }

    private static class ViewHolder {
        private TextView emojiTv;
        private ImageView emojiImage;
    }
    /**
     * 表情点击监听器
     */
    public interface OnEmojiClickListener {
        void onClick(int position);
    }
}
